package com.endava.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * Maps the table from database with the bean class.
 * 
 * @author acojocar
 * 
 */
@Entity
@Table(name = "LOGIN", uniqueConstraints = { @UniqueConstraint(columnNames = "ID") })
public class Login {

	@Id
	@GeneratedValue(generator = "gen")
	@GenericGenerator(name = "gen", strategy = "foreign", parameters = @Parameter(name = "property", value = "employee"))
	@Column(name = "ID", unique = true, nullable = false)
	private int id;

	@Column(name = "USERNAME")
	private String username;

	@Column(name = "PASSWORD")
	private String password;

	@Column(name = "USER_ROLE")
	private String userRole;

	@OneToOne(mappedBy = "login", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	private Employee employee;

	public Login() {
	}

	public Login(int id, String userRole, String username, String password, Employee employee) {
		this.id = id;
		this.userRole = userRole;
		this.username = username;
		this.password = password;
		this.employee = employee;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserRole() {
		return userRole;
	}

	public void setUserRole(String userRole) {
		this.userRole = userRole;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	@Override
	public String toString() {
		return "Spring MVC - Hibernate emp: " + username + ", " + password;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Login) {
			Login login = (Login) obj;

			if (this.username.equals(login.getUsername()) && this.password.equals(login.getPassword())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.username.hashCode() + this.password.hashCode();
	}

}
